package com.qcby.onlinelearningplatform.Controller;


import com.qcby.onlinelearningplatform.Common.R;
import com.qcby.onlinelearningplatform.Common.ResultCommon;
import com.qcby.onlinelearningplatform.Entity.*;
import com.qcby.onlinelearningplatform.Service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/student")
@Api(tags = "学生接口信息")
public class StudentController {

    @Autowired
    public StudentService studentService;

    /*@GetMapping("/login")
    @ResponseBody
    @ApiOperation(value = "学生登录接口",notes = "登录方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "s_sno",value = "学号",required = true),
            @ApiImplicitParam(name = "s_password",value = "密码",required = true)
    })
    public R<Student> login(HttpServletRequest request, Student student){
        String sno = student.getS_sno();
        String password = student.getS_password();
        Student stu = studentService.login(sno);

        System.out.println(stu);

        if(null == stu){
            return R.error("账号不存在");
        }

        if(!stu.getS_password().equals(password)){
            return R.error("密码错误");
        }
        request.getSession().setAttribute("student",stu.getS_name());
        return R.success(stu);


    }*/

    @GetMapping("/login")
    @ResponseBody
    @ApiOperation(value = "学生登录接口",notes = "登录方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "s_sno",value = "学号",required = true),
            @ApiImplicitParam(name = "s_password",value = "密码",required = true)
    })
    public ResultCommon login(HttpServletRequest request, Student student){
        String sno = student.getS_sno();
        String password = student.getS_password();
        Student stu = studentService.login(sno);

        System.out.println(stu);

        if(null == stu){
            return ResultCommon.error("账号不存在");
        }

        if(!stu.getS_password().equals(password)){
            return ResultCommon.error("密码错误");
        }
        request.getSession().setAttribute("student",stu.getS_name());
        return ResultCommon.success("登录成功",stu);

    }

    @PostMapping("/studentChannel")
    @ResponseBody
    @ApiOperation(value = "查询学生后台栏目信息")
    public ResultCommon channel(){
        List<StudentChannel> stuChannel =  studentService.studentChannel();
        System.out.println(stuChannel);
        return ResultCommon.success("查询成功",stuChannel);
    }

    @PostMapping("/studentTopic")
    @ResponseBody
    @ApiOperation(value = "查询今日话题信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每一页的数量",required = true),
            @ApiImplicitParam(name = "pageIndex",value = "页码",required = true)
    })
    public ResultCommon topic(Integer pageSize,Integer pageIndex){
        List<Topic> stuTopic =  studentService.studentTopic(pageSize,pageIndex);
        System.out.println(stuTopic);
        return ResultCommon.success("查询成功",stuTopic);
    }

    @PostMapping("/topicLikeTitle")
    @ResponseBody
    @ApiOperation(value = "搜索今日话题信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每一页的数量",required = true),
            @ApiImplicitParam(name = "pageIndex",value = "页码",required = true),
            @ApiImplicitParam(name = "title",value = "题目",required = true)
    })
    public ResultCommon topicLikeTitle(Topic topic){
        List<Topic> topicMap =  studentService.topicLikeTitle(topic);
        System.out.println(topicMap);
        return ResultCommon.success("搜索成功",topicMap);
    }

    @PostMapping("/insertTopic")
    @ResponseBody
    @ApiOperation(value = "添加今日话题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment",value = "评论",required = false),
            @ApiImplicitParam(name = "content",value = "内容",required = false),
            @ApiImplicitParam(name = "title",value = "题目",required = true)
    })
    public ResultCommon insertTopic(Topic topic){
        topic.setTime(new Date());
        int code  = studentService.insertTopic(topic);


        if(code == 1){
            return ResultCommon.success("添加成功");
        }
        return ResultCommon.error("添加失败");
    }


    @PostMapping("/studentClassfile")
    @ResponseBody
    @ApiOperation(value = "查询课件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每一页的数量",required = true),
            @ApiImplicitParam(name = "pageIndex",value = "页码",required = true)
    })
    public ResultCommon studentClassfile(Integer pageSize,Integer pageIndex){
        List<ClassFile> stuClassfile =  studentService.studentClassfile(pageSize,pageIndex);
        System.out.println(stuClassfile);
        if (0 == stuClassfile.size()){
            return ResultCommon.error("没有查询到课件");
        }
        return ResultCommon.success("查询成功",stuClassfile);
    }

    @PostMapping("/classfileLikeName")
    @ResponseBody
    @ApiOperation(value = "搜索课件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每一页的数量",required = true),
            @ApiImplicitParam(name = "pageIndex",value = "页码",required = true),
            @ApiImplicitParam(name = "fileName",value = "课件名",required = true)
    })
    public ResultCommon classfileLikeName(ClassFile classFile){
        List<ClassFile> classfileMap =  studentService.classfileLikeName(classFile);
        System.out.println(classfileMap);
        if (0 == classfileMap.size()){
            return ResultCommon.error("抱歉，未查询到该课件");
        }
        return ResultCommon.success("搜索课件成功",classfileMap);
    }


    @PostMapping("/studentHomework")
    @ResponseBody
    @ApiOperation(value = "查询作业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每一页的数量",required = true),
            @ApiImplicitParam(name = "pageIndex",value = "页码",required = true)
    })
    public ResultCommon studentHomework(Integer pageSize,Integer pageIndex){
        List<HomeWork> stuHomework =  studentService.studentHomework(pageSize,pageIndex);
        System.out.println(stuHomework);
        if (0 == stuHomework.size()){
            return ResultCommon.error("没有查询到作业");
        }
        return ResultCommon.success("查询成功",stuHomework);
    }

    @PostMapping("/homeworkLikeName")
    @ResponseBody
    @ApiOperation(value = "搜索作业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每一页的数量",required = true),
            @ApiImplicitParam(name = "pageIndex",value = "页码",required = true),
            @ApiImplicitParam(name = "workName",value = "作业名",required = true)
    })
    public ResultCommon homeworkLikeName(HomeWork homeWork){
        List<HomeWork> homeWork1 =  studentService.homeworkLikeName(homeWork);
        System.out.println(homeWork1);
        if (0 == homeWork1.size()){
            return ResultCommon.error("抱歉，未查询到该课件");
        }
        return ResultCommon.success("搜索课件成功",homeWork1);
    }






}
